layui.use([ 'form', 'element','jquery'], function() {
    var form = layui.form,
        element = layui.element;
    var $=layui.jquery;
    var province = $(".province");

    //初始将省份数据赋予
    for (var i = 0; i < provinceList.length; i++) {
        addEle(province, provinceList[i].name);
    }

    //向select中 追加内容
    function addEle(ele, value) {
        var optionStr = "";
        optionStr = "<option value=" + value + " >" + value + "</option>";
        ele.append(optionStr);
    }
    //重新渲染select
    form.render('select');

    //移除select中所有项
    function removeEle(ele) {
        ele.find("option").remove();
        var optionStar = "<option value=''>" + "请选择" + "</option>";
        ele.append(optionStar);
    }

    var provinceText,
        cityText,
        cityItem;
    //选定省份后 将该省份的数据读取追加上
    form.on('select(province)', function(data) {
        var city = $(data.elem).parents(".layui-form-item").find(".city"),
            district = $(data.elem).parents(".layui-form-item").find(".district");
        provinceText = data.value;
        $.each(provinceList, function(i, item) {
            if (provinceText == item.name) {
                cityItem = i;
                return cityItem;
            }
        });
        removeEle(city);
        removeEle(district);
        $.each(provinceList[cityItem].cityList, function(i, item) {
            addEle(city, item.name);
        })
        form.render('select');
    })

    //选定后 将对应的数据读取追加上
    form.on('select(city)', function(data) {
        var district = $(data.elem).parents(".layui-form-item").find(".district");
        cityText = data.value;
        removeEle(district);
        $.each(provinceList, function(i, item) {
            if (provinceText == item.name) {
                cityItem = i;
                return cityItem;
            }
        });
        $.each(provinceList[cityItem].cityList, function(i, item) {
            if (cityText == item.name) {
                for (var n = 0; n < item.areaList.length; n++) {
                    addEle(district, item.areaList[n]);
                }
            }
        })
        form.render('select');
    })
    //选定后 将对应的数据读取追加上
    form.on("select(district)",function(data){
        //alert("徐州你："+data.value);
        //省
        var sheng=$('#province1 option:selected').text();
        //市
        var shi=$('#city1 option:selected').text();
        //区
        //var qu=$('#district1 option:selected').text();
        $('#detailedAddress1').val(sheng+"-"+shi+"-"+data.value+"-");
    });
    //收货人地址
    form.on("select(district1)",function(data){
        //alert("徐州你："+data.value);
        //省
        var sheng=$('#province option:selected').text();
        //市
        var shi=$('#city option:selected').text();
        //区
       // var qu=$('#district1 option:selected').text();
        $('#detailedAddress').val(sheng+"-"+shi+"-"+data.value+"-");
    });


    //计算基本运费
    form.on("select(xcl)",function(){
        //得到计费方式
        if( $('#tollcollectionmanner option:selected').val()=="重量"){
            //赋值
            $('#basic').val($('#weight').val()*$('#unitprice').val()*$('#quantity').val());
        }else if ($('#tollcollectionmanner option:selected').val()=="体积"){
            //赋值
            $('#basic').val($('#volume').val()*$('#unitprice').val()*$('#quantity').val());

        }else if ($('#tollcollectionmanner option:selected').val()=="件数"){
            //赋值
            $('#basic').val($('#quantity').val()*$('#unitprice').val());
            //应收总金额
        }

        sum();




    });
    function  sum() {
        //存储总值
        var a=1;
        $(".aggregateamount").each(function(){
            //判断是否为null和kong
            if($(this).val()!=""&&$(this).val()!=null){
                a+=parseInt($(this).val());
            }
        })
        $('#aggregateamount').val(parseInt($('#basic').val())+parseInt(a)-1);
    }


})